package com.cnswhy.cloud.server.merchant.controller.customer;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnswhy.cloud.common.core.base.request.PageParams;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.core.entity.BuyBackOrder;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.CustomerPrivilege;
import com.cnswhy.cloud.server.core.dto.CustomerPrivilegeSaveDTO;
import com.cnswhy.cloud.server.core.dto.CustomerPrivilegeUpdateDTO;
import com.cnswhy.cloud.server.core.dto.CustomerPrivilegePageDTO;
import com.cnswhy.cloud.server.core.service.customer.CustomerPrivilegeService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.service.spell.SpellPriceService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
//import com.github.zuihou.security.annotation.PreAuth;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-16
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/customerPrivilege")
@Api(value = "CustomerPrivilege", tags = "")
//@PreAuth(replace = "customerPrivilege:")
public class CustomerPrivilegeController extends SuperController<CustomerPrivilegeService, Long, CustomerPrivilege, CustomerPrivilegePageDTO, CustomerPrivilegeSaveDTO, CustomerPrivilegeUpdateDTO> {
    @Autowired
    CustomerService customerService;
    @Autowired
    SpellPriceService spellPriceService;

    @Override
    public void handlerQueryParams(PageParams<CustomerPrivilegePageDTO> params) {
        if(StringUtils.isNotBlank(params.getModel().getLoginName())){
            Customer customer = customerService.getOne(Wraps.<Customer>lbQ().eq(Customer::getLoginName,params.getModel().getLoginName()));
            params.getModel().setCustomerId(customer.getId());
        }
    }

    @Override
    public void handlerResult(IPage<CustomerPrivilege> page) {
        for(CustomerPrivilege customerPrivilege : page.getRecords()){
            customerPrivilege.setCustomer(customerService.getById(customerPrivilege.getCustomerId()));
            customerPrivilege.setSpellPrice(spellPriceService.getById(customerPrivilege.getPriceId()));
        }
    }

    @PostMapping("/queryCan")
    public R<Integer> queryCan(Long priceId, Long customerId){
        return success(baseService.queryCanUse(priceId,customerId));
    }
    @PostMapping("/queryCustomerPrivilegeList")
    public R<List<CustomerPrivilege>> queryCustomerPrivilegeList(Long priceId, Long customerId){
        return success(baseService.queryCustomerPrivilegeList(priceId,customerId));
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<CustomerPrivilege> customerPrivilegeList = list.stream().map((map) -> {
            CustomerPrivilege customerPrivilege = CustomerPrivilege.builder().build();
            return customerPrivilege;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(customerPrivilegeList));
    }
}
